<?php
    $url = new Url();
    $arr_status = LoadConfig::$status;
    //$data_code = ARingCode::getDataCode($ringId); 
    $current_url = Common::getCurrentUrl();
    $path_paging = strpos($current_url,'?') !==false ? $current_url.'&' : $current_url.'?';
    $path_paging = str_replace("?page=".$page."&","?",$path_paging);
    $path_paging = str_replace("&page=".$page."&","&",$path_paging);
?>
<script type="text/javascript">
    function ajaxQuickUpdate(id){
        var status = 0;var lyrics = 0;var hit = 0;
        if($("#status_"+id).is(":checked")){status = 1; }
        var strUrl = "<?=$url->createUrl("ringBack/ajaxQuickUpdate") ?>";
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                download : $("#download_"+id).val(),
                hit : $("#hit_"+id).val(),
                status:status,
                id:id
            },
            success: function(msg){
                if(msg == 1){
                    alert('Sửa thành công');
                    location.reload();
                }else{
                    alert(msg);
                }
            }          
        });
    }
    function ajaxDelete(id){
        var strUrl = "<?=$url->createUrl("ringBack/ajaxDelete") ?>";
        if(confirm('Xóa file này thì tất cả các dữ liệu liên quan sẽ mất hết! Bạn có muốn tiếp tục không ?')){
            $.ajax({
                type: "POST",
                url: strUrl,
                data: {
                    id:id, 
                },
                success: function(msg){
                    if(msg == 1){
                        alert('Xóa thành công');
                        location.reload();
                    }else{
                        alert(msg);
                    }
                }          
            });
        }
    }
</script>
<div id="container">
    <div class="main clearfix">
        <div class="Tabs clearfix">
            <ul class="clearfix">
                <li><a class="active" href="<?php echo Url::createUrl('ringBack/index');?>"><span>Quản trị nhạc chờ</span></a></li>
                <li><a href="<?php echo Url::createUrl('ringBack/singer');?>"><span>Quản trị ca sĩ/nhóm nhạc</span></a></li>
                <li><a href="<?php echo Url::createUrl('ringBack/category');?>"><span>Quản trị thể loại nhạc</span></a></li>
            </ul>                
        </div>
        <div class="box clearfix bottom30">
            <form name="SearchForm" method="GET"> 
                <ul class="form4">
                    <li class="clearfix"><label><strong>Từ khóa </strong>:</label>
                        <div class="filltext">
                            <input type="text" style="width:331px;margin-right:10px" value="<?php echo $keyword?>" name="keyword">
                            <span>Theo:</span>
                            <select style="width:150px;" name="type">
                                <option value="2" <?php echo ($type == 2 ? 'selected="selected"':'' )?>> -- Tên bài hát --</option>
                                <option value="1" <?php echo ($type == 1 ? 'selected="selected"':'' )?>>ID</option>
                            </select>
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Thể loại </strong>:</label>
                        <div class="filltext">
                            <select style="width:150px;" name="categoryId">
                                <option value=""> -- Chọn thể loại --</option>
                                <?php foreach($data_categoryId as $key=>$value){?> 
                                    <option value="<?php echo $key;?>" <?php echo ($categoryId == $key ? 'selected="selected"':'' )?>><?php echo $value?></option>
                                    <?php }?>   
                            </select>
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Tình trạng </strong>:</label>
                        <div class="filltext">
                            <select style="width:150px;" name="status">
                                <option value="2"> -- Tất cả --</option>
                                <?php foreach($arr_status as $key=>$value){?>
                                    <option value="<?php echo $key;?>" <?php echo ($status == $key ? 'selected="selected"':'' )?>><?php echo $value?></option>
                                    <?php }?>
                            </select>    
                            <select style="width:150px;" name="lyrics">
                                <option value="2"> -- Lời bài hát --</option>
                                <option value="1" <?php echo ($lyrics == 1 ? 'selected="selected"':'' )?>>Có</option>
                                <option value="0" <?php echo ($lyrics == 0 ? 'selected="selected"':'' )?>>Không</option>
                            </select>            
                        </div>
                    </li>
                    <li class="clearfix"><label><strong>Lượt tải </strong>:</label>
                        <div class="filltext">
                            <select style="width:150px;" name="order_download">
                                <option value="0"> -- Mặc định --</option>
                                <option value="1" <?php echo ($order_download == 1 ? 'selected="selected"':'' )?>>Tăng dần</option>
                                <option value="2" <?php echo ($order_download == 2 ? 'selected="selected"':'' )?>>Giảm dần</option>
                            </select>    
                        </div>
                    </li>
                    <li class="clearfix"><label>&nbsp;</label>
                        <div class="filltext">
                            <input type="submit" value="Tìm kiếm" class="btn-bigblue"> 
                        </div>
                    </li>
                </ul>
            </form>
        </div>
        <div class="box">
            <span class="btn-blue"><a href="<?php echo $url->createUrl("ringBack/create")?>"><strong>Thêm nhạc chờ</strong></a></span>  
        </div>
        <div class="box">
            <div class="fillter clearfix">
                <div class="fl">
                    Tìm thấy <strong class="clred"><?php echo $count; ?></strong> kết quả - 
                </div>               
                <div class="fr">
                    <ul class="paging">
                        <?php
                            echo Paging::show_paging_cp($max_page,$page,$path_paging);
                        ?>
                    </ul>
                </div>
            </div>
            <div class="table clearfix">
                <table width="100%" cellspacing="0" cellpadding="0" border="0">
                    <tbody>
                        <tr class="bg-grey">
                            <td width="4%"><strong>ID</strong></td>
                            <td width="7%"><strong>Thể loại</strong></td>
                            <td width="12%"><strong>Tên bài hát</strong></td>                            
                            <td width="18%"><strong>Mã nhạc chờ</strong></td> 
                            <td width="10%"><strong>Ca sĩ/nhóm nhạc</strong></td>
                            <td width="6%"><strong>Trạng thái</strong></td>
                            <td width="6%"><strong>Lượt xem</strong></td>
                            <td width="6%"><strong>Lượt tải</strong></td>
                            <td width="8%"><strong>Ngày đăng</strong></td>
                            <td width="7%"><strong>Hành động</strong></td>
                        </tr>
                        <?php
                        $telco=LoadConfig::$telco; 
                        foreach($data as $key=>$value){
                        $code = isset($data_code[$value["id"]]) ? $data_code[$value["id"]] : array();  
                        ?>    
                            <tr>
                                <td><strong><?php echo $value["id"]?></strong></td>
                                <td><strong><?php echo $data_categoryId[$value["categoryId"]]?></strong></td>
                                <td><strong class="clred"><?php echo $value["title"]?></strong></td>                                
                                <td>
                                    <?php
                                    $viettel = ''; 
                                    $mobilefone = '';
                                    $vina = '';
                                    $Vietnam = '';
                                    $Beeline = ''; 
                                    foreach($code as $telco=>$codes){
                                        if($telco == "1"){
                                            foreach($codes as $c){
                                                $viettel .= " " . $c;
                                            }                                            
                                        } else if($telco == "2"){
                                            foreach($codes as $c){
                                                $mobilefone .= " " . $c;
                                            }                                            
                                        } else if($telco == "3"){
                                            foreach($codes as $c){
                                                $vina .= " " . $c;
                                            }                                            
                                        }else if($telco == "4"){
                                            foreach($codes as $c){
                                                $Vietnam .= " " . $c;
                                            }                                            
                                        }else if($telco == "5"){
                                            foreach($codes as $c){
                                                $Beeline .= " " . $c;
                                            }                                            
                                        } 
                                    }
                                    ?>
                                    <strong>
                                        Viettel:(<?php echo $viettel;?>)
                                        Mobilefone: (<?php echo $mobilefone;?>)
                                        Vinaphone: (<?php echo $vina;?>) 
                                        Vietnam: (<?php echo $Vietnam;?>)
                                        Beeline: (<?php echo $Beeline;?>)  
                                    </strong>
 
                                </td> 
                                <td><strong><?php echo $value["name"]?></strong></td>  
                                <td><input type="checkbox" value="1" id="status_<?php echo $value["id"]?>" <?php echo $value["status"]==1 ? 'checked="checked"':'';?>> Hiển thị</td>
                                <td><strong id="hit_<?php echo $value["id"]?>"><?php echo $value["hit"]?></strong></td>
                                <td><strong id="download_<?php echo $value["id"]?>"><?php echo $value["download"]?></strong></td>
                                <td><?php echo date("d-m-Y H:i:s", $value["create_date"]);?></td>
                                <td>
                                    <a class="s14" href="javascript:void(0)" onclick="ajaxQuickUpdate('<?php echo $value["id"]?>')"> Lưu </a> | 
                                    <a href="<?php echo $url->createUrl("ringBack/edit",array("id"=>$value["id"]))?>"> Sửa </a> |
                                <a href="javascript:void(0)" onclick="ajaxDelete('<?php echo $value["id"]?>')">  Xóa  </a> </td>
                            </tr>
                            <?php }?>  
                    </tbody>
                </table>
            </div>
            <div class="fillter clearfix">
                <div class="fr">
                    <ul class="paging">
                        <?php
                            echo Paging::show_paging_cp($max_page,$page,$path_paging);
                        ?>
                    </ul>
                </div>
            </div>

        </div>
    </div>
    <!-- End main-->

    </div>